<!-- pages/followup/list.vue -->
<template>
	<view class="follow-list-page">
		<!-- 置顶区域 -->
		<scroll-view v-if="pinnedRecord.length" class="pinned-container" scroll-y>
			<view class="section-title">
				<uni-icons type="star-filled" size="14" color="#f5a623" />
				<text>置顶记录</text>
			</view>
			<view v-for="item in pinnedRecord" :key="item.id" class="pinned-card">
				<view class="card-header">
					<text class="name">{{ item.createBy }}</text>
					<text class="date">{{ formatTime(item.createTime) }}</text>
					<view class="card-btn">
						<u-button size="mini" type="warning" @click="toggleTop(item, 0)">取消置顶</u-button>
					</view>
				</view>
				<view class="card-content">{{ item.followRemark }}</view>
			</view>
			<uni-load-more v-if="loadingPinned" status="loading" />
		</scroll-view>
		<view v-else class="pinnedRecord-empty"></view>

		<!-- 历史记录 -->
		<scroll-view class="history-container" scroll-y @scrolltolower="loadMoreHistory">
			<text class="section-title">历史记录</text>
			<view v-if="recordList.length" class="history-list">
				<view v-for="(item, index) in recordList" :key="item.id || index" class="history-item">
					<view class="item-header">
						<view class="meta">
							<uni-icons type="checkmarkempty" size="16" color="#1890ff" />
							<text class="name">{{ item.createBy }}</text>
							<text class="time">{{ formatTime(item.createTime) }}</text>
						</view>
						<view v-if="editingKey !== (item.id || index)" class="actions">
							<text class="edit" @click="startEdit(item, index)">编辑</text>
							<text class="top" @click="toggleTop(item, 1)">置顶</text>
						</view>
						<view v-else class="actions">
							<text class="confirm" @click="confirmEdit(item)">确认</text>
							<text class="cancel" @click="cancelEdit">取消</text>
						</view>
					</view>
					<view class="item-content">
						<textarea v-if="editingKey === (item.id || index)" v-model="editText" auto-height class="edit-textarea" />
						<text v-else>{{ item.followRemark }}</text>
					</view>
				</view>
				<uni-load-more v-if="loadingHistory" status="loading" />
				<view v-if="noMore && !loadingHistory && recordList.length > 5" class="no-more">没有更多记录了</view>
			</view>

			<view v-else class="empty-box">
				<u-empty text="暂无跟进记录" mode="history"></u-empty>
			</view>
		</scroll-view>

		<!-- 快捷添加按钮（可选） -->
		<view class="fab" @click="openForm">
			<uni-icons type="plusempty" size="24" color="#fff" />
		</view>
	</view>
</template>

<script>
import { listFollowRecord, updateFollowRecord } from '@/api/follow/index.js';
import { setToast } from '@/common/utils';

export default {
	data() {
		return {
			pinnedRecord: [],
			recordList: [],
			loadingPinned: false,
			loadingHistory: false,
			noMore: false,
			pageTitle: '跟进记录',

			editingKey: null,
			editText: '',

			pageParams: {
				customerId: 0,
				orderId: 0,
				typeCode: 1
			},
			pinnedPage: { pageNum: 1, pageSize: 5 },
			historyPage: { pageNum: 1, pageSize: 10 }
		};
	},
	onLoad(options) {
		const { row } = options;
		this.pageParams = JSON.parse(row);
		this.pageTitle = this.pageParams.typeCode === 1 ? '客服跟进' : '销售跟进';
		this.loadPinned();
		this.loadHistory();

		// 监听提交成功事件
		uni.$on('followSubmitted', () => {
			this.resetLoad();
			this.loadPinned();
			this.loadHistory();
		});
	},
	onUnload() {
		uni.$off('followSubmitted');
	},
	methods: {
		openForm() {
			uni.navigateTo({
				url: `/pages/followup/form?params=${encodeURIComponent(JSON.stringify(this.pageParams))}`
			});
		},
		resetLoad() {
			this.pinnedRecord = [];
			this.recordList = [];
			this.pinnedPage.pageNum = 1;
			this.historyPage.pageNum = 1;
			this.noMore = false;
		},
		async loadPinned() {
			if (this.loadingPinned) return;
			this.loadingPinned = true;
			try {
				const res = await listFollowRecord({
					...this.pageParams,
					topStatus: 1,
					pageNum: this.pinnedPage.pageNum,
					pageSize: this.pinnedPage.pageSize
				});
				if (res.code === 200 && Array.isArray(res.rows)) {
					this.pinnedRecord.push(...res.rows);
					if (res.rows.length < this.pinnedPage.pageSize) {
						// no more
					} else {
						this.pinnedPage.pageNum++;
					}
				}
			} catch (err) {
				console.error('加载置顶失败:', err);
			} finally {
				this.loadingPinned = false;
			}
		},
		async loadHistory() {
			if (this.loadingHistory || this.noMore) return;
			this.loadingHistory = true;
			try {
				const res = await listFollowRecord({
					...this.pageParams,
					topStatus: 0,
					pageNum: this.historyPage.pageNum,
					pageSize: this.historyPage.pageSize
				});
				if (res.code === 200 && Array.isArray(res.rows)) {
					this.recordList.push(...res.rows);
					if (res.rows.length < this.historyPage.pageSize) {
						this.noMore = true;
					} else {
						this.historyPage.pageNum++;
					}
				}
			} catch (err) {
				console.error('加载历史失败:', err);
			} finally {
				this.loadingHistory = false;
			}
		},
		loadMoreHistory() {
			this.loadHistory();
		},
		toggleTop(item, topStatus) {
			updateFollowRecord({ id: item.id, topStatus }).then((res) => {
				if (res.code === 200) {
					setToast(topStatus === 1 ? '已置顶' : '已取消');
					this.resetLoad();
					this.loadPinned();
					this.loadHistory();
				}
			});
		},
		startEdit(item, index) {
			this.editingKey = item.id || index;
			this.editText = item.followRemark;
		},
		cancelEdit() {
			this.editingKey = null;
			this.editText = '';
		},
		confirmEdit(item) {
			updateFollowRecord({ id: item.id, followRemark: this.editText }).then((res) => {
				if (res.code === 200) {
					item.followRemark = this.editText;
					this.cancelEdit();
					uni.showToast({ title: '更新成功' });
				}
			});
		},
		formatTime(time) {
			return time ? time.split('.')[0].replace('T', ' ') : '';
		}
	}
};
</script>

<style lang="scss">
$primary: #1890ff;
$warn: #f5a623;
$text: #333;
$gray: #999;
$border: #eee;
$bg: #f8f9fa;

.follow-list-page {
	height: 100vh;
	background: $bg;
	box-sizing: border-box;
	display: flex;
	flex-direction: column;
}

.page-header {
	background: $primary;
	color: #fff;
	padding: 60rpx 30rpx 30rpx;
	display: flex;
	justify-content: space-between;
	align-items: center;
	font-size: 36rpx;
	font-weight: 600;
	box-sizing: border-box;
	border-bottom-left-radius: 20rpx;
	border-bottom-right-radius: 20rpx;
	box-shadow: 0 4rpx 20rpx rgba(24, 144, 255, 0.2);
}

.pinned-container {
	width: 710rpx;
	height: 270rpx;
	padding: 20rpx;
	background: #fffaf3;
	margin: 20rpx;
	border-radius: 16rpx;
	box-sizing: border-box;
	box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
}

.pinnedRecord-empty {
	height: 20rpx;
}

.section-title {
	font-size: 30rpx;
	color: #666;
	margin-bottom: 20rpx;
	display: flex;
	align-items: center;
	gap: 10rpx;
	box-sizing: border-box;
	font-weight: 500;
}

.pinned-card {
	background: #fff;
	border-radius: 16rpx;
	padding: 28rpx;
	box-sizing: border-box;
	margin-bottom: 20rpx;
	box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
}

.pinned-card .card-header {
	display: flex;
	align-items: center;
	justify-content: space-between;
	font-size: 28rpx;
	margin-bottom: 16rpx;

	.card-btn {
		margin-left: auto;
	}
}

.pinned-card .name {
	font-weight: 600;
	color: $text;
}

.pinned-card .date {
	font-size: 24rpx;
	color: $gray;
	margin: 0 16rpx;
}

.history-container {
	flex: 1;
	padding: 0 20rpx;
}

.history-list {
	padding-bottom: 30rpx;
}

.history-item {
	background: #fff;
	border-radius: 16rpx;
	padding: 30rpx;
	margin-bottom: 20rpx;
	box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
}

.item-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 20rpx;
}

.meta {
	display: flex;
	align-items: center;
	gap: 12rpx;
	font-size: 28rpx;
	color: $text;
}

.meta .name {
	font-weight: 500;
}

.meta .time {
	font-size: 24rpx;
	color: $gray;
}

.actions {
	display: flex;
	align-items: center;
	gap: 20rpx;
	font-size: 26rpx;
}

.actions text {
	padding: 0 10rpx;
}

.actions .edit,
.actions .top {
	color: $primary;
}

.actions .confirm {
	color: #52c41a;
}

.actions .cancel {
	color: $gray;
}

.item-content {
	color: #555;
	line-height: 1.6;
	font-size: 28rpx;
}

.edit-textarea {
	width: 100%;
	border: 1px solid #e8e8e8;
	padding: 20rpx;
	border-radius: 8rpx;
	font-size: 28rpx;
	background: #fafafa;
}

.no-more {
	text-align: center;
	color: $gray;
	font-size: 26rpx;
	padding: 20rpx;
}

.empty-box {
	text-align: center;
	color: $gray;
	padding: 100rpx 0;
}

.fab {
	position: fixed;
	bottom: 60rpx;
	right: 40rpx;
	width: 80rpx;
	height: 80rpx;
	background: $primary;
	border-radius: 50%;
	display: flex;
	justify-content: center;
	align-items: center;
	box-shadow: 0 6rpx 20rpx rgba(24, 144, 255, 0.3);
	z-index: 999;
}
</style>
